<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul,
        ol,
        li {
            list-style: none;
        }

        img {
            width: 100%;
            height: 100%;
            display: block;
        }

        .banner {
            width: 100%;
            height: 500px;
            position: relative;
            margin: 50px 0;
        }

        .banner>ul {
            width: 100%;
            height: 100%;
            position: relative;
        }

        .banner>ul>li {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
            transition: all .5s linear;
        }

        .banner>ul>li.active {
            opacity: 1;
        }

        .banner>ol {
            width: 200px;
            height: 30px;
            position: absolute;
            left: 30px;
            bottom: 30px;
            background-color: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: space-around;
            align-items: center;
            border-radius: 15px;

        }

        .banner>ol>li {
            width: 20px;
            height: 20px;
            background-color: white;
            border-radius: 50%;
            cursor: pointer;
        }

        .banner>ol>li.active {
            background-color: orange;
        }

        .banner>div {
            width: 40px;
            height: 40px;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background-color: rgba(0, 0, 0, 0.5);
            /* text-align: center;
            line-height: 40px; */
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 30px;
            color: #fff;
        }

        .banner>div.left {
            left: 0;
        }

        .banner>div.right {
            right: 0;
        }
    </style>
</head>

<body>


    <div class="banner">

        <ul class="imgBox">
            <li class="active"><img src="./img/1.webp" alt=""></li>
            <li><img src="./img/2.webp" alt=""></li>
            <li><img src="./img/3.webp" alt=""></li>
            <li><img src="./img/4.png" alt=""></li>
        </ul>

        <!-- 焦点区域 -->
        <ol>
            <li data-i="0" data-name="poinr" class="active"></li>
            <li data-i="1" data-name="poinr"></li>
            <li data-i="2" data-name="poinr"></li>
            <li data-i="3" data-name="poinr"></li>
        </ol>
        <!-- 左右切换按钮 -->

        <div class="left">&lt;</div>
        <div class="right">&gt;</div>
    </div>
    <script>
        // 获取到所有承载图片的li 和焦点li
        var imgs = document.querySelectorAll('ul>li')
        var points = document.querySelectorAll('ol>li')
        var banner = document.querySelector('.banner')
        // 准备一个变量 表示当前是第几张 默认是0 默认显示的是索引第0张
        var index = 0

        // 书写切换一张的函数
        /**
         *  约定
         * 参数为 true 切换下一张
         * 参数为 false 切换上一张
         * 参数为 数字 切换指定索引的一张
         * 
        */
        function changeOne(type) {
            // 1、让当前这一张消失
            imgs[index].className = ''
            points[index].className = ''
            // 2、根据type 传递参数，来切换index的值
            if (type === true) {
                index++
            } else if (type === false) {
                index--
            } else {
                index = type
            }
            // 判断index 的边界值
            if (index >= imgs.length) {
                index = 0;
            }
            if (index < 0) {
                index = imgs.length - 1
            }

            // 3 让改变后的这一张显示出来
            imgs[index].className = 'active'
            points[index].className = 'active'

        }

        // 给轮播图区域绑定点击事件
        banner.onclick = function (e) {

            // 判断点击的是左按钮
            if (e.target.className === 'left') {
                // 点击的是左按钮
                changeOne(false)
            }
            if (e.target.className == 'right') {
                // 点击的是右按钮
                changeOne(true)
            }
            // 判断点击的是焦点按钮
            if (e.target.dataset.name == "poinr") {

                var num = e.target.dataset.i - 0
                changeOne(num)
            }
        }

        setInterval(() => {
            changeOne(true)
        }, 3000);

    </script>

</body>

</html>